import { Injectable } from '@angular/core';
import {
  HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HttpResponse
} from '@angular/common/http';
import { tap } from 'rxjs/operators';

@Injectable()
export class HttpClientInterceptor implements HttpInterceptor {
  intercept(req: HttpRequest<any>, next: HttpHandler) {
    const modifyReq = req.clone({
      setHeaders: {
        token: 'aaaa-xxxx-ssss-dss'
      }
    });
    return next.handle(modifyReq).pipe(
      tap((event: HttpEvent<any>) => {
        if (event instanceof HttpResponse && event.status === 200) {
          const data = event.body;
          console.log('响应数据为：', data);
          console.log('响应ok');
        }
      })
    );
  }
}
